import router from '@/routers/index'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Layout from '@/layout/index.vue'
import { useUserStore } from './store/modules/user'
NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/auth-redirect'] // 设置白名单
// 记录路由
let hasRoles = true

router.beforeEach(async (to, from, next) => {
  const _useUserStore = useUserStore()
  // 开启进度条
  NProgress.start()
  // 设置标题
  if (typeof to.meta.title === 'string') {
    document.title = to.meta.title || '首页'
  }
  if (_useUserStore.token && to.name === 'Login') {
    next('/list')
  } else if (to.meta.login && !_useUserStore.token) {
    next('/login')
  } else {
    // 确定用户是否已登录过，存在Token
    next()
  }
})

router.afterEach(() => {
  NProgress.done()
})
